package com.example.do_an_haputech;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlaceJSONParser {
	public List<HashMap<String, String>> parse(JSONObject jObject) {
	JSONArray jPlaces = null;
		try {
			jPlaces = jObject.getJSONArray("results");
			
		}catch(JSONException e){
			e.printStackTrace();
		}
	
	return getPlaces(jPlaces);
	}

	private List<HashMap<String, String>> getPlaces(JSONArray jPlaces) {
		// TODO Auto-generated method stub
		int placesCount = jPlaces.length();
		List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
		HashMap<String, String> places = null;
		for(int i= 0; i<placesCount;i++){
			try{
				places = getPlace((JSONObject) jPlaces.get(i));
				placesList.add(places);
			}catch(JSONException e){
				e.printStackTrace();
			}
		}
		
		return placesList;
	}

	private HashMap<String, String> getPlace(JSONObject jPlace) {
		// TODO Auto-generated method stub
		HashMap<String, String> place = new HashMap<String, String>();
		String placeName = "-NA-";
        String vicinity="-NA-";
        String latitude="";
        String longitude="";
        try {
            // Extracting Place name, if available
            if(!jPlace.isNull("name")){
                placeName = jPlace.getString("name");
            }
 
            // Extracting Place Vicinity, if available
            if(!jPlace.isNull("vicinity")){
                vicinity = jPlace.getString("vicinity");
            }
 
            latitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat");
            longitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");
 
            place.put("place_name", placeName);
            place.put("vicinity", vicinity);
            place.put("lat", latitude);
            place.put("lng", longitude);
 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return place;
    }
}